﻿using ICSharpCode.SharpZipLib.Zip;
using System;
using System.IO;
using System.Threading.Tasks;

namespace DalamudManager
{
    public static class Zip
    {
        public static string UnZipFile(string targetFile, string outputDir)
            => UnZipFile(File.OpenRead(targetFile.Trim()), outputDir);
        public static string UnZipFile(Stream targetFile, string outputDir)
        {
            string rootFile = " ";
            try
            {
                ZipInputStream s = new ZipInputStream(targetFile);
                ZipEntry theEntry;
                string path = outputDir;
                string rootDir = " ";
                while ((theEntry = s.GetNextEntry()) != null)
                {
                    rootDir = Path.GetDirectoryName(theEntry.Name);
                    if (rootDir.IndexOf("\\") >= 0)
                    {
                        rootDir = rootDir.Substring(0, rootDir.IndexOf("\\") + 1);
                    }
                    string dir = Path.GetDirectoryName(theEntry.Name);
                    string fileName = Path.GetFileName(theEntry.Name);
                    if (dir != " ")
                    {
                        if (!Directory.Exists(outputDir + "\\" + dir))
                        {
                            path = outputDir + "\\" + dir;
                            Directory.CreateDirectory(path);
                        }
                    }
                    else if (dir == " " && fileName != "")
                    {
                        path = outputDir;
                        rootFile = fileName;
                    }
                    else if (dir != " " && fileName != "")
                    {
                        if (dir.IndexOf("\\") > 0)
                        {
                            path = outputDir + "\\" + dir;
                        }
                    }
                    if (dir == rootDir)
                    {
                        path = outputDir + "\\" + rootDir;
                    }
                    if (fileName != string.Empty)
                    {
                        FileStream streamWriter = File.Create(path + "\\" + fileName);
                        int size = 2048;
                        byte[] data = new byte[2048];
                        while (true)
                        {
                            size = s.Read(data, 0, data.Length);
                            if (size > 0)
                            {
                                streamWriter.Write(data, 0, size);
                            }
                            else
                            {
                                break;
                            }
                        }
                        streamWriter.Close();
                    }
                }
                s.Close();
                return rootFile;
            }
            catch (Exception ex)
            {
                return "1; " + ex.Message;
            }
        }
    }
}